package LeetCode.Stack;

import java.util.Stack;

/**
 * @Author:galaxy_yan
 * @Date: 2022/10/23
 * @Time: 12:17
 * @Description
 */
public class isValid {
    public static  boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if(ch==')'){
                if(!stack.empty() &&stack.peek()=='('){
                    stack.pop();
                }else {
                    stack.push(ch);
                }
            }else if(ch==']'){
                if( !stack.empty() &&stack.peek()=='['){
                    stack.pop();
                }else {
                    stack.push(ch);
                }
            }else if(ch=='}'){
                if(!stack.empty() &&stack.peek()=='{'){
                    stack.pop();
                }else {
                    stack.push(ch);
                }
            }else{
                stack.push(ch);
            }
        }
        if(stack.empty() ){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        String s="}";
        System.out.println(isValid(s));
    }
}
